package com.example.rabbitmqdemo.mq;

import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * @author liujb
 * @description TODO RabbitMQ消息监听消费
 * @date 2022-06-30 17:53
 */
@Component
public class RabbitMQListener {

    // 监听hello队列消息并处理
    @RabbitListener(queues = "hello")
    public void helloConsumer(String message) {
        System.out.println("======>message:" + message);
    }

    @RabbitListener(queues = "work")
    public void workConsumer1(String message){
        System.out.println("======>workConsumer1"+message);
    }

    @RabbitListener(queues = "work")
    public void workConsumer2(String message){
        System.out.println("======>workConsumer2"+message);
    }

    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue, // 临时队列
                    exchange = @Exchange(value = "logs_start", type = "fanout") //指定交换机和交换机类型
            )
    })
    public void fanoutConsumer1(String message) {
        System.out.println("======>fanoutConsumer1:"+message);
    }

    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue, // 临时队列
                    exchange = @Exchange(value = "logs_start", type = "fanout") //指定交换机和交换机类型
            )
    })
    public void fanoutConsumer2(String message) {
        System.out.println("======>fanoutConsumer2:"+message);
    }


    @RabbitListener(bindings = {
            @QueueBinding(value = @Queue, // 队列
                    exchange = @Exchange(value = "directs", type = "direct"), // 交换机 type默认为direct
                    key = {"info", "error", "warning"} // 绑定的路由键或模式。多个元素将导致多个绑定
            )
    })
    public void routingConsumer1(String message) {
        System.out.println("========> routingConsumer1:" + message);
    }

    @RabbitListener(bindings = {
            @QueueBinding(value = @Queue,
                    exchange = @Exchange(value = "directs"),
                    key = {"error"})
    })
    public void routingConsumer2(String message) {
        System.out.println("=======> routingConsumer2:" + message);
    }


    @RabbitListener(
            bindings = @QueueBinding(value = @Queue,
                    exchange = @Exchange(value = "topics.test", type = "topic"),
                    key = {"user.*"})
    )
    public void topicConsumer1(String message) {
        System.out.println("=====> topicConsumer1:" + message);
    }

    @RabbitListener(bindings = {
            @QueueBinding(value = @Queue,
                    exchange = @Exchange(value = "topics.test", type = "topic"),
                    key = {"user.#","order.#"})
    })
    public void topicConsumer2(String message) {
        System.out.println("====> topicConsumer2:" + message);
    }



}
